package com.zjitc.service.impl;

import com.zjitc.dao.CatDao;
import com.zjitc.model.Cat;
import com.zjitc.service.CatService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.sql.Date;
import java.util.List;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/19 14:10
 * @description:
 */
@Service
public class CatServiceImpl implements CatService {

  @Autowired
  private CatDao catDao;

  @Override
  public List<Cat> findByCatSpecies(String catSpecies) {
    return catDao.findByCatSpecies(catSpecies);
  }

  @Override
  public void addCats() {
    List<Cat> catList = new ArrayList<>();
    List list = new ArrayList();
    list.add("英短");
    list.add("美短");
    list.add("暹罗");
    list.add("阿比");
    list.add("褴褛");
    list.add("布偶");
    List list1 = new ArrayList();
    list1.add(2999.0);
    list1.add(3999.0);
    list1.add(1999.0);
    list1.add(5999.0);
    list1.add(8999.0);
    List list2 = new ArrayList();
    list2.add("images/sold01.jpg");
    list2.add("images/sold02.jpg");
    list2.add("images/sold03.jpg");
    list2.add("images/sold04.jpg");
    list2.add("images/sold05.jpg");
    list2.add("images/sold06.jpg");
    list2.add("images/sold07.jpg");
    list2.add("images/sold08.jpg");
    list2.add("images/sold09.jpg");
    list2.add("images/sold10.jpg");
    list2.add("images/sold11.jpg");
    for (int i = 0; i < 54; i++) {
      Cat cat = new Cat();
/*cat.setCatName("w");*/


      //id
      cat.setId(i + 1801190000);
      //birthday
      int year = 2018;
      int month = 1 + new Random().nextInt(4);
      int day = new Random().nextInt(30);
      String string = year + "-0" + month + "-" + day;
      SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
      try {
        Date date = (Date) sf.parse(string);
        cat.setBirthday(date);
        cat.setUpdateTime(date);
      } catch (ParseException e) {
        e.printStackTrace();
      }
      //UpdateTime


      /*随即生成整数*/
      //int randNumber = new Random().nextInt(100-0+1)+0;
      //user.setAge(randNumber);
      int count = (int) (Math.random() * list.size());
      int count1 = (int) (Math.random() * list1.size());
      int cout2 = (int) (Math.random() * list2.size());
      cat.setPrice((Double) list1.get(count1));
      cat.setCatSpecies(String.valueOf(list.get(count)));
      cat.setImage(String.valueOf(list2.get(cout2)));
      //Vaccine
      if (month <= 2) {
        cat.setVaccine(0);
      } else {
        cat.setVaccine(2);
      }
      if (month >= 3) {
        cat.setExpellingParasite("一次");
      } else {
        cat.setExpellingParasite("月份不足");
      }
      //sex
      if (i % 2 == 0) {
        cat.setSex("公");
      } else {
        cat.setSex("母");
      }

      //catSpecies
//      System.out.println(cat);
      catList.add(cat);
      //catDao.addCats(cat);

    }
    /*catDao.findByCatSpecies("英短");*/
    catDao.addCats(catList);


  }

  @Override
  public int chcekSellByCatid(Integer catid) {
    return catDao.chcekSellByCatid(catid);
  }

  @Override
  public Cat findByCatId(Integer catid) {
    return catDao.findByCatId(catid);
  }

  @Override
  public void changeCatSell(int num, Integer catid) {
    catDao.changeCatSell(num,catid);
  }

  @Override
  public void changeHost(Integer catid, String username) {
    catDao.changeHost(catid,username);
  }

  @Override
  public List<Cat> showByUsername(String username) {
    return catDao.showByUsername(username);
  }

  @Override
  public int num(String username) {
    return catDao.num(username);
  }

  @Override
  public int price(String username) {
    return catDao.price(username);
  }

}
